package cn.zhizihua.controller;


import cn.zhizihua.exception.AppException;
import cn.zhizihua.exception.AppExceptionCodeMsg;
import cn.zhizihua.pojo.Disease;
import cn.zhizihua.pojo.MedicalRecordDisease;
import cn.zhizihua.service.IMedicalRecordDiseaseService;
import cn.zhizihua.vo.ResultBean;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  病例记录控制器
 * </p>
 *
 * @author zhizihua
 * @since 2024-12-18
 */
@RestController
@RequestMapping("/recorddisease")
public class MedicalRecordDiseaseController {

    @Autowired
    IMedicalRecordDiseaseService medicalRecordDiseaseService;

    /**
     * 通过 病历id查询对应疾病
     * @param mrid
     * @return
     */
    @GetMapping("/mrid/{mrid}")
    public ResultBean getByMrId(@PathVariable("mrid") Integer mrid){
        MPJLambdaWrapper<MedicalRecordDisease> wrapper = new MPJLambdaWrapper<>();
        wrapper.setAlias("mrd")
                .selectAll(MedicalRecordDisease.class,"mrd")
                .selectAs(Disease::getDiseaseName,"'disease.diseaseName'")
                .selectAs(Disease::getDiseaseICD,"'disease.diseaseICD'")
                .leftJoin(Disease.class,"d",Disease::getId,MedicalRecordDisease::getDiseaseId)
                .eq("mrd.medical_record_id",mrid);

        List<MedicalRecordDisease> list = medicalRecordDiseaseService.list(wrapper);
        return ResultBean.SUCCESS(list);
    }

    /**
     * 添加西医诊断 疾病类型
     * @param medicalRecordDiseases
     * @return
     */
    @PostMapping("/addbatch")
    public ResultBean addbatch(@RequestBody List<MedicalRecordDisease> medicalRecordDiseases){
        System.out.println("MedicalRecordDiseaseController--------addbatch");
        if (medicalRecordDiseases==null || medicalRecordDiseases.size()==0){
            throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
        }

        boolean b = medicalRecordDiseaseService.saveBatch(medicalRecordDiseases);
        if (!b){
            throw new AppException(AppExceptionCodeMsg.INSERT_FAIL);
        }
        return ResultBean.SUCCESS();
    }


    /**  TODO 目前可正常运行 （可以优化）
     * 删除medicalRecordDisease 病例和疾病
     * 用于处理西医诊断错误添加疾病
     * @return
     */
    @PostMapping
    public ResultBean del(@RequestBody MedicalRecordDisease medicalRecordDisease){
        System.out.println("删除西医诊断"+medicalRecordDisease);
        // 病历ID
        Integer medicalRecordId = medicalRecordDisease.getMedicalRecordId();
        // 疾病ID
        Integer diseaseId = medicalRecordDisease.getDiseaseId();

        //DELETE FROM medical_record_disease WHERE medical_record_id=14 AND disease_id=987
        QueryWrapper<MedicalRecordDisease> wrapper = new QueryWrapper<>();
        wrapper.eq("medical_record_id",medicalRecordId)
                .eq("disease_id",diseaseId);
        boolean remove = medicalRecordDiseaseService.remove(wrapper);

        if (!remove){
            //删除失败 抛出异常
            throw new AppException(AppExceptionCodeMsg.DELETE_FAIL);
        }
        return ResultBean.SUCCESS();
    }
}
